package com.example.yls.demoa;

import android.media.MediaPlayer;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button btnPlay;
    private Button btnPause;
    private Button btnStop;
    private SeekBar seekbar;
    private MediaPlayer mediaPlayer = new MediaPlayer();;
    private Boolean isPuase=false;
    private int MSG_PROGRESS_CHANGED=1001;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPlay = findViewById(R.id.btn_play);
        btnPause = findViewById(R.id.btn_pause);
        btnStop = findViewById(R.id.btn_stop);
        seekbar = findViewById(R.id.seek_bar);


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if (mediaPlayer.isPlaying()){
                        int duration = mediaPlayer.getDuration();
                        int position = mediaPlayer.getCurrentPosition();
                        int progress = position * 100 / duration;
                        seekbar.setProgress(progress);

                        Message message = new Message();
                        message.what = MSG_PROGRESS_CHANGED;
                        message.arg1 = position;
                        message.arg2 = duration;

                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();



        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(isPuase){
                    mediaPlayer.start();
                    isPuase=false;
                }
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource("/mnt/shared/Other/xiaoqingwa.mp3");
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mediaPlayer.start();
                    }
                });
                mediaPlayer.prepareAsync();
            }
        });
        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer.isPlaying()&&mediaPlayer!=null){
                    mediaPlayer.pause();
                    isPuase = true;
                }
            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                }
            }
        });
    }

}
